package com.mmall.controller.backend;


import com.google.common.collect.Maps;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.dao.UserMapper;
import com.mmall.pojo.Product;
import com.mmall.pojo.User;
import com.mmall.service.IFileService;
import com.mmall.service.IProductService;
import com.mmall.service.IUserService;
import com.mmall.utils.PropertiesUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/manager/product/")
public class ManageProductController {


    @Autowired
    private IUserService iUserService;

    @Autowired
    private IProductService iProductService;

    @Autowired
    private IFileService iFileService;

    @RequestMapping(value = "list.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse list(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                               HttpSession session) {
        ServerResponse response = iUserService.checkAdminRole(session);
        if (!response.is_success()) {
            return response;
        }

        //获取所有的产品信息
        return iProductService.listForManager(pageNum, pageSize);

    }


    @RequestMapping(value = "search.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse search(String productName, Integer productId,
                                 @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, HttpSession session) {
        ServerResponse response = iUserService.checkAdminRole(session);
        if (!response.is_success()) {
            return response;
        }
        return iProductService.searchProduct(productName, productId, pageNum, pageSize);
    }

    @RequestMapping(value = "detail.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse detail(Integer productId, HttpSession session) {
        ServerResponse response = iUserService.checkAdminRole(session);
        if (!response.is_success()) {
            return response;
        }
        return iProductService.detail(productId);

    }


    @RequestMapping(value = "set_sale_status.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse setSaleStatus(Integer productId, Integer status, HttpSession session) {
        ServerResponse response = iUserService.checkAdminRole(session);
        if (!response.is_success()) {
            return response;
        }
        return iProductService.setSaleStatus(productId, status);
    }


    @RequestMapping(value = "save_or_update.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse saveOrUpdate(Product product, HttpSession session) {
        ServerResponse response = iUserService.checkAdminRole(session);
        if (!response.is_success()) {
            return response;
        }
        return iProductService.saveOrUpdateProduct(product);
    }


    @RequestMapping(value = "delete.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse delete(Integer productId, HttpSession session) {
        ServerResponse response = iUserService.checkAdminRole(session);
        if (!response.is_success()) {
            return response;
        }
        return iProductService.delete(productId);
    }

    @RequestMapping(value = "richtext_img_upload.do", method = RequestMethod.GET)
    @ResponseBody
    public Map richtextImgUpload(@RequestParam(value = "upload_file", required = false)
                                         MultipartFile file, HttpServletRequest request,
                                 HttpServletResponse response, HttpSession session) {
        ServerResponse serverResponse = iUserService.checkAdminRole(session);
        Map resultMap = Maps.newHashMap();
        if (serverResponse == null) {
            resultMap.put("success", false);
            resultMap.put("msg", "请登录管理员");
            return resultMap;
        }

        //富文本中对于返回值有自己的要求,我们使用是simditor所以按照simditor的要求进行返回
//        {
//            "success": true/false,
//                "msg": "error message", # optional
//            "file_path": "[real file path]"
//        }

        if (serverResponse.is_success()) {
            //获取路径
            String path = request.getSession().getServletContext().getRealPath("upload");
            //上传文件
            String uploadFileName = iFileService.upload(file, path);
            if (StringUtils.isBlank(uploadFileName)) {
                resultMap.put("success", false);
                resultMap.put("msg", "请登录管理员");
                return resultMap;
            }

            String url = PropertiesUtil.getProperty("ftp.server.http.prefix") + uploadFileName;
            resultMap.put("success", true);
            resultMap.put("msg", "上传成功");
            resultMap.put("file_path", url);
            //添加相应头，很重要的一步
            response.addHeader("Access-Control-Allow-Headers", "X-File-Name");
            return resultMap;
        } else {
            resultMap.put("success", false);
            resultMap.put("msg", "无权限操作");
            return resultMap;
        }


    }


}
